package com.guan.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.ruiji.common.BaseContext;
import com.guan.ruiji.common.R;
import com.guan.ruiji.pojo.Employee;
import com.guan.ruiji.service.EmployeeService;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author guanzhenjie
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 登录
     * @param request
     * @param employee
     * @return
     */
    @RequestMapping("login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String username = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getUsername, username);
        Employee emp = employeeService.getOne(lambdaQueryWrapper);

        if (emp == null){
            return R.error("该用户不存在");
        }
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        if(emp.getStatus() == 0){
            return R.error("账户已禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @RequestMapping("register")
    public R<String> register(HttpServletRequest request, @RequestBody Employee employee){

        BaseContext.setEmployeeId((Long)request.getSession().getAttribute("employee"));

        // 设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        // 保存
        boolean success = employeeService.save(employee);

        if (success) {
            return R.success("新增员工成功");
        } else {
            return R.success("新增员工失败");
        }
    }

    /**
     * 查询员工
     * @param name
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("getEmployeeList")
    public R<Page> getEmployeeList(@RequestParam("name") String name, @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize){
        // 分野构造器
        Page page = new Page(currentPage, pageSize);
        // 查询
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);
        lambdaQueryWrapper.orderByDesc(Employee::getStatus);
        lambdaQueryWrapper.orderByAsc(Employee::getId);

        employeeService.page(page, lambdaQueryWrapper);
        return R.success(page);
    }


    /**
     * 更新员工
     * @param employee
     * @return
     */
    @RequestMapping("update")
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){

        BaseContext.setEmployeeId((Long)request.getSession().getAttribute("employee"));

        // 更新
        boolean success = employeeService.updateById(employee);

        if (success) {
            return R.success("更新员工成功");
        } else {
            return R.success("更新员工失败");
        }
    }
}
